<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Category;
use App\lib\TreeClass;

class CategoryController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Category $category)
    {
        $categorys = $category->all()->toArray();
        $categorys = TreeClass::tree($categorys, $title = 'name', $fieldPri = 'id', $fieldPid = 'pid');
        return view('admin.category.index', compact('categorys'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.category.create');
    }

    /*添加子栏目*/
    public function addSon($id, Category $category)
    {
        //全部栏目
        $categorys = $category->all()->toArray();
        $categorys = TreeClass::tree($categorys, $title = 'name', $fieldPri = 'id', $fieldPid = 'pid');
        return view('admin.category.addson', compact('categorys', 'id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Category $category)
    {
        $category->pid = $request->pid;
        $category->name = $request->name;
        $category->describe = $request->describe;
        $category->save();
        return redirect('/admin/category')->with('status', '添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $current = Category::find($id);
        $categorys = Category::all()->toArray();
        $categorys = TreeClass::tree($categorys, $title = 'name', $fieldPri = 'id', $fieldPid = 'pid');
        return view('admin.category.edit', compact('categorys', 'current'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $category = Category::find($id);
        $category->pid = $request->pid;
        $category->name = $request->name;
        $category->describe = $request->describe;
        $category->save();
        return redirect('/admin/category')->with('status', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $hasSon = Category::where('pid', $id)->count();
        if ($hasSon > 0) {
            return redirect('/admin/category')->with('status', '请删除子栏目');
        }
        Category::destroy($id);
        return redirect('/admin/category')->with('status', '删除成功');
    }
}
